# Copyright (c) 2021 Presto Labs Pte. Ltd.
# Author: xguo

import json
import sys
import time

import tornado.ioloop
import tornado.web
from dydx3.starkex.order import SignableOrder


class MainHandler(tornado.web.RequestHandler):
  def get(self):
    print(self.request.body)
    self.write("Hello, world")

  def post(self):
    print(self.request.body)
    data = json.loads(self.request.body)
    privkey = data.pop("privkey")
    data["network_id"] = 1
    data["limit_fee"] = "0.0015"
    order_to_sign = SignableOrder(**data)
    order_signature = order_to_sign.sign(privkey)
    self.write(order_signature)


def make_app():
    return tornado.web.Application([
        (r"/", MainHandler),
    ])

if __name__ == "__main__":
    app = make_app()
    app.listen(9876)
    tornado.ioloop.IOLoop.current().start()
